using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace eYap.src
{
    public class SettingsManager
    {
        static string FileName = "Accounts.cfg";
        public enum Protocol { Facebook = 0 };
        public static List<Account> ACCOUNTS = new List<Account>();

        public static void ReadAccounts()
        {
            if (File.Exists(FileName))
            {
                using (XmlTextReader reader = new XmlTextReader(FileName))
                {
                    while (reader.Read())
                    {
                        if (reader.Name == SettingsManager.Protocol.Facebook.ToString())
                        {
                            XmlReader rd = reader.ReadSubtree();
                            while (rd.Read())
                            {
                                XmlDocument doc = new XmlDocument();
                                string xml = rd.ReadInnerXml();
                                doc.LoadXml(xml);
                                ACCOUNTS.Add(new Account(doc.FirstChild.Name, Cryptor.Decrypt(doc.FirstChild.InnerText, true), 
                                             SettingsManager.Protocol.Facebook));
                            }
                        }
                    }
                }
            }
        }

        public static void SaveAccountSettings(Account account)
        {
            if (!File.Exists(FileName))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter wr = XmlWriter.Create(FileName, settings);
                wr.WriteStartDocument();
                wr.WriteStartElement("Profiles");
                wr.WriteStartElement(account.protocol.ToString());
                wr.WriteElementString(account.username, Cryptor.Encrypt(account.password, true));
                wr.WriteEndElement();
                wr.WriteEndElement();
                wr.WriteEndDocument();
                wr.Close();
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FileName);
                XmlNode profiles = doc.LastChild;
                XmlElement profile = doc.CreateElement(account.protocol.ToString());
                XmlElement newProfile = doc.CreateElement(account.username);
                newProfile.InnerText = Cryptor.Encrypt(account.password, true);
                profile.AppendChild(newProfile);
                profiles.AppendChild(profile);
                doc.Save(FileName);

            }

        }

        public static void RemoveAccount(string username)
        {
            Account acc_toDelete = new Account();
            File.Delete(FileName);
            foreach (Account acc in ACCOUNTS)
            {
                if (acc.username != username)
                {
                    SaveAccountSettings(acc);
                }
                else
                {
                    acc_toDelete = acc;
                }
            }
            ACCOUNTS.Remove(acc_toDelete);
        }
    }

    public struct Account
    {
        public string username;
        public string password;
        public SettingsManager.Protocol protocol;

        public Account(string uname, string pass, SettingsManager.Protocol ptcl)
        {
            username = uname;
            password = pass;
            protocol = ptcl;
        }
    }
}
